package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhx.iservice.IMemberService;
import com.yhx.mapper.MemberMapper;
import com.yhx.pojo.Member;
import com.yhx.utils.*;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements IMemberService {
    @Resource
    private MemberMapper memberMapper;
//    @Override
//    public PageResult pagelist(QueryPageBean queryPageBean) {
//        QueryWrapper<Member> queryWapper = new QueryWrapper<>();
//        String queryString = queryPageBean.getQueryString();
//        if(queryString!=null&&queryString.length()>0){
//            queryWapper.like("name",queryString).or().like("phoneNumber", queryString);
//        }
//        //创建分页对象
//        Page<Member> memberPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
//        Page<Member> userPage = memberMapper.selectPage(memberPage, queryWapper);
//        return new PageResult(userPage.getTotal(), userPage.getRecords());
//    }

    @Override
    public PageResult pagelist2(QueryPageBean queryPageBean) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        String sex=queryPageBean.getSex();
        if(!sex.equals("不限")){
            queryWrapper.eq("sex", sex);
        }
        Integer startAge=null;
        Integer endAge=null;
        //年龄范围
        String age = queryPageBean.getAge();
        if(!age.equals("不限")){
            List<Integer> ages = AgeUtils.getAges(age);
            for (int i = 0; i < ages.size(); i++) {
               startAge= ages.get(0);
               endAge=ages.get(1);
            }
            //求出出生年龄的范围
            Calendar calendar = Calendar.getInstance();
            //当前年份
            int i = calendar.get(Calendar.YEAR);
            Integer startYear=i-endAge;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

            Integer endYear=i-startAge;
            Date parse=null;
            Date parse1=null;

            try {
                parse = sdf.parse(String.valueOf(startYear));
                parse1 = sdf.parse(String.valueOf(endYear));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.between("birthday", parse, parse1);
        }
        //创建时间
        if(!queryPageBean.getCreatetime().equals("不限")){
            queryWrapper.like("regTime", queryPageBean.getCreatetime());
        }
        String queryString = queryPageBean.getQueryString();
        if(queryString!=null&&queryString.length()>0){
            queryWrapper.like("name",queryString).or().like("phoneNumber", queryString);
        }
        //创建分页对象
        Page<Member> memberPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Member> userPage = memberMapper.selectPage(memberPage, queryWrapper);
        return new PageResult(userPage.getTotal(), userPage.getRecords());
    }

    @Override
    //注释因为这里rpc连接超时，在controller中的@Reference(timeout = 60000)
    //高可用
//    public Result getCreTimeList() {
//        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
//
//        List<Member> memberPage = memberMapper.selectList(queryWrapper);
//
//        HashSet<Integer> set = new HashSet<>();
//        for (Member record : memberPage) {
//            Calendar instance = Calendar.getInstance();
//            Date regtime = record.getRegtime();
//            instance.setTime(regtime);
//            Integer i = instance.get(Calendar.YEAR);
//            set.add(i);
//        }
//        ArrayList<Integer> list = new ArrayList<>();
//        for (Integer integer : set) {
//            list.add(integer);
//        }
//        System.out.println(list);
//        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,list);
//
//
//    }
    //创建会员的年份列表
    public Result getCreTimeList(){
        //List<Date> regtime = memberMapper.getRegtime();
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        //求出最早的时间
        memberQueryWrapper.select("MIN(regTime) as regtime");
        Member member = memberMapper.selectOne(memberQueryWrapper);
        //得到最早创建创建会员的时间
        Date regtime = member.getRegtime();
        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(regtime);
        //最早的创建会员的年份
        int startYear= instance.get(Calendar.YEAR);
        instance.setTime(date);
        //当前年
        int endYear= instance.get(Calendar.YEAR);
        ArrayList<String> integers = new ArrayList<>();
        for (int i=startYear; i <=endYear ; i++) {
            integers.add(String.valueOf(i));
        }

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,integers);
    }

    @Override
    public Result save(Member member) {
        member.setRegtime(new Date());
        member.setPassword("123456");
        int insert = memberMapper.insert(member);
        if(insert>0){
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
    }

    @Override
    public Result getByid(int id) {
        Member member = memberMapper.selectById(id);
        if(member!=null){
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS,member);
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }

    @Override
    public Result update(Member member) {
        int i = memberMapper.updateById(member);
        if(i>0){
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }

    @Override
    public Result delete(int id) {
        int i = memberMapper.deleteById(id);
        if(i>0){
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
    }

    @Override
    public Result deleteAll(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        int i = memberMapper.deleteBatchIds(list);
        if(i>0){
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
    }

    @Override
    public Long getTodayNewMember() {
        try {
            String s = DateUtils.parseDate2String(DateUtils.getToday(), "yyyy-MM-dd");
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper.eq("regTime",s);
            return memberMapper.selectCount(memberQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Long getTotalMember() {
        return memberMapper.selectCount(null);
    }

    @Override
    public Long getThisWeekNewMember() {
        try {
            //获取周一
            String monday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday(), "yyyy-MM-dd");
            String today = DateUtils.parseDate2String(DateUtils.getToday(), "yyyy-MM-dd");
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper.between("regTime",monday,today);
            return memberMapper.selectCount(memberQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Long getThisMonthNewMember() {
        try {
            //获取每月的1号
            String monday = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth(), "yyyy-MM-dd");
            String today = DateUtils.parseDate2String(DateUtils.getToday(), "yyyy-MM-dd");
            QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper.between("regTime",monday,today);
            return memberMapper.selectCount(memberQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public List<Map<String, Object>> sexbili() {
        return memberMapper.sexbili();
    }

    @Override
    public List<Long> between() {
        ArrayList<Long> list = new ArrayList<>();
        for (int i = 3; i>=0; i--) {
            //求到每一个季度的开始年月
            Date startDate = DateUtils.getBeforQuarterStartDate(new Date(), i);
            String newStartDate = new SimpleDateFormat("yyyy-MM").format(startDate);
            Calendar instance = Calendar.getInstance();
            instance.setTime(startDate);
            //季度开始的月份往后推迟两个月得到该季度的最后一个月
            instance.add(Calendar.MONTH,2);
            Date endTime = instance.getTime();
            String newEndTime = new SimpleDateFormat("yyyy-MM").format(endTime);
            Long between = memberMapper.between(newStartDate+"-01", newEndTime+"-31");
            list.add(between);
        }
        return list;
    }

    @Override
    public List<Long> sum() {
        ArrayList<Long> longs = new ArrayList<>();
        for (int i = 3; i >=0; i--) {
            Date startDate = DateUtils.getBeforQuarterStartDate(new Date(), i);
//            当前是4月  2022-4
            String newStartDate = new SimpleDateFormat("yyyy-MM").format(startDate);
            Calendar instance = Calendar.getInstance();
            instance.setTime(startDate);
            instance.add(Calendar.MONTH,2);
            Date endTime = instance.getTime();
            String newEndTime = new SimpleDateFormat("yyyy-MM").format(endTime);
            Long sum = memberMapper.sum(newEndTime + "-31");
            longs.add(sum);
        }
        return longs;
    }

}
